package com.huanzhu.asuper.supervisionapp.cjbjdudao.view;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by lihao on 2018/4/21
 */

public class UserNameSplitUtil {
    public static String subName(String text) {
        if (TextUtils.isEmpty(text)) return "";
        String subName = "";
        //字母或数字
        if (isLetter(text) || isNumeric(text)) {
            subName = text.substring(0, 1).toUpperCase();
            //中文
        } else if (isChinese(text)) {
            subName = text.substring(0, 1);
        }
        return subName;
    }

    /**
     * 判断首位是否是字母
     */
    private static boolean isLetter(String s) {
        if (TextUtils.isEmpty(s)) return false;
        char c = s.charAt(0);
        int i = (int) c;
        return (i >= 65 && i <= 90) || (i >= 97 && i <= 122);
    }
    /**
     * 判断首位是否是数字
     */
    private static boolean isNumeric(String str) {
        if (TextUtils.isEmpty(str)) return false;
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;

    }
    /**
     * 判断首位是否是汉字
     */
    private static boolean isChinese(String str) {
        if (TextUtils.isEmpty(str)) return false;
        String substring = str.substring(0, 1);
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(substring);
        return m.matches();

    }
}
